-- sql.lua
-- STRICTLY SERVER-SIDE

require( "mysql" )

-- Database connection details
local hostname = "localhost"
local port = nil
local username = "root"
local password = ""
local database = "trurp"

function SQLConnect()
	db, err = mysql.connect( hostname, username, password, database, port )
	if (db == 0) then
		print("[TruRP] Error connecting to database! (" .. err .. ")")
	end
end
hook.Add( "Initialize", "TruRPInitialize", SQLConnect )

function SQLDisconnect()
	local succ, err = mysql.disconnect( db )
	if not succ then
		print("[TruRP] Error disconnecting from database! (" .. err .. ")")
	end
end
hook.Add( "ShutDown", "TruRPShutDown", SQLDisconnect )

function SQLQuery( query, type )
	type = type or mysql.QUERY_FIELDS

	result, succ, err = mysql.query(db, query, type)
	if not succ then
		print("[TruRP] Error querying database! (" .. err .. ")")
		GAMEMODE:ShutDown()
		return
	end

	return result
end